package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.data.PartnerCheckstandData;
import com.banzhi.shop.model.CallBack.PartnerCheckstandCallBack;
import com.banzhi.shop.model.IPartnerCheckstandModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/6/27.
 */
/*支付方式 24微信支付 22支付宝 支付 1余额支付*/
public class PartnerCheckstandImpl implements IPartnerCheckstandModel {
    //余额支付
    @Override
    public void PayYure(String paytype, final PartnerCheckstandCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.PartnersApplyURL);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("paytype","1");

        LogUtils.d("PayYue", params.toString());

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        PartnerCheckstandData YuEData = gson.fromJson(result, PartnerCheckstandData.class);
                        callBack.onPayYuePartnerSuccess(YuEData);
                    } else {
                        callBack.onPayYuePartnerError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PayYuE", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });

    }
    //微信支付
    @Override
    public void PayWX(String paytype, final PartnerCheckstandCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.PartnersApplyURL);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));

        params.addBodyParameter("paytype", "24");
        LogUtils.d("PayWX", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        PartnerCheckstandData partnerCheckstandWXData = gson.fromJson(result, PartnerCheckstandData.class);
                        callBack.onPayWXPartnerSuccess(partnerCheckstandWXData);
                    } else {
//                        callBack.onPayWXSuccess(jsonobject);
                        callBack.onPayWXPartnerError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PayWX_Partner", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    //支付宝支付
    @Override
    public void PayZFB(String paytype, final PartnerCheckstandCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.PartnersApplyURL);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("paytype", "22");

        LogUtils.d("PayZFB", params.toString());

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        PartnerCheckstandData ZFBData = gson.fromJson(result, PartnerCheckstandData.class);
                        callBack.onPayZFBPartnerSuccess(ZFBData);
                    } else {
                        callBack.onPayZFBPartnerError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("PayZFB", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
